package cn.quang.search.toolkit.component.items.impl.aggregation;

import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 聚合条件实体
 */
public class ESAggregationCondition {

    private final Map<ESAggregationConditionEnum, Set<String>> conditionMap = new HashMap<>();

    public void addAggregationCondition(String col, ESAggregationConditionEnum conditionEnum){
        Set<String> columns = this.conditionMap.computeIfAbsent(conditionEnum, key -> new HashSet<>());
        columns.add(col);
    }

    /**
     * 聚合查询列
     * @return 查询列SQL
     */
    public String getAggregationColumnSql(){
        List<String> columnSqlList = new ArrayList<>();
        for (Map.Entry<ESAggregationConditionEnum, Set<String>> entry : conditionMap.entrySet()) {
            columnSqlList.add(entry.getKey().getAggregationColumnSql(new ArrayList<>(entry.getValue())));
        }
        return StringUtils.join(columnSqlList, ", ");
    }

    /**
     * 聚合条件列
     * @return 条件列SQL
     */
    public String getAggregationConditionSql() {
        List<String> conditionSqlList = new ArrayList<>();
        for (Map.Entry<ESAggregationConditionEnum, Set<String>> entry : conditionMap.entrySet()) {
            conditionSqlList.add(entry.getKey().getAggregationConditionSql(new ArrayList<>(entry.getValue())));
        }
        return StringUtils.join(conditionSqlList, ", ");
    }
}
